import Dialog from '../../lib/vant/dialog/dialog';
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    key: "",
    currentPage: 1,
    pageSize: 6,
    notfind: 0,
    courses: []
  },

  onSearch: function (event) {

    this.setData({
      key: event.detail
    })
    wx.request({
      url: 'http://120.77.36.131:8180/STULive/getCourseList.action',
      data: {
        value: this.data.key,
        currentPage: 1,
        pageSize: 6
      },
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: res => {
        console.log(res.data)
        if (res.data.length > 0) {
          for (var i = 0; i < res.data.length; i++) {
            res.data[i].courseName = res.data[i].courseName.split(']')[1];
          }
          this.setData({
            courses: res.data,
            nofind: 0
          })
        } else {
          this.setData({
            nofind: 1
          })
          Dialog.alert({
            title: '亲爱的同学啊',
            message: '系统找不到相关的课程啊，换个关键词吧~'
          }).then(() => {

          });
        }
      }
    })
  },
  onLoad: function () {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#FF8C00',
    })
    wx.setNavigationBarTitle({
      title: "蹭课查询"
    })
  },
  onReachBottom() {
    if (this.data.courses.length < 6) {

    } else {
      this.setData({
        currentPage: this.data.currentPage + 1
      })
      wx.showLoading({
        title: '玩命加载中',
        mask: true
      })
      wx.request({
        url: 'http://120.77.36.131:8180/STULive/getCourseList.action',
        data: {
          value: this.data.key,
          currentPage: this.data.currentPage,
          pageSize: 6
        },
        method: "get",
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: res => {
          console.log(res.data)
          if (res.data.length > 0) {
            for (var i = 0; i < res.data.length; i++) {
              res.data[i].courseName = res.data[i].courseName.split(']')[1];
            }
            var length = this.data.courses.length;
            var nowcourses = [];
            for(var i =0;i<length;i++){
              nowcourses.push(this.data.courses[i]);
            }
            for (var i = 0; i < res.data.length; i++) {
             nowcourses.push(res.data[i])
            }

            this.setData({
              courses:nowcourses
            })
          } else {
            this.setData({
              currentPage: this.data.currentPage - 1
            })
          }
          wx: wx.hideLoading()
        }
      })
    }
  }
})
